/**
 * @author 小泽玛丽罗
 */

var tmpl_followinf_friend='<div class="ub max-h5" style="border-bottom:1px solid black;min-height:3.5em;">'
					+'<div id="${uid}" class="ub-f1 max-h4 max-w4 umar-a" onclick="PerCenter(this)"><img style="width:100%;" src="${avatar_url}"></div>'
					+'<div id="${uid}" class="ub-f3 min-w7 ub-ver umar-a" onclick="PerCenter(this)">'
						+'<div class="ub-f3 ub-ver" >'
						+'<div class="ub">${nickname}</div>'
						+'<div class="ub umh2 ">'
							+'<div class="ub-f1 umh2 ${sexPic}"></div>'
							+'<div class="ub-f2 ver-c ulev-1">粉丝:${follower_count}</div>'
						+'</div>'
					+'</div>'
					+'</div>'
					+'<div class="ub-f1 ub-ver ub" style="margin-right:0.3em;margin-bottom:0.2em;">'
						+'<div class="umh2"></div>'
						+'<div id="${uid}" name="${uid}" class="ub-f1 min-w5 max-w5 tx-c ulev t-wh c-wh ver-c1" onclick="attention(this)" style="background-color:rgb(117, 113, 113);max-height: 1.7em;min-height:1.7em;">取消关注</div>'
					+'</div>'
					+'</div>'
				+'</div>';
var tmpl_mutulFollow_friend='<div class="ub max-h5" style="border-bottom:1px solid black;min-height:3.5em;">'
					+'<div id="${uid}" class="ub-f1 max-h4 max-w4 umar-a" onclick="PerCenter(this)"><img style="width:100%;" src="${avatar_url}"></div>'
					+'<div id="${uid}" class="ub-f3 min-w7 ub-ver umar-a" onclick="PerCenter(this)">'
						+'<div class="ub-f3 ub-ver" >'
						+'<div class="ub">${nickname}</div>'
						+'<div class="ub umh2 ">'
							+'<div class="ub-f1 umh2 ${sexPic}"></div>'
							+'<div class="ub-f2 ver-c ulev-1">粉丝:${follower_count}</div>'
						+'</div>'
					+'</div>'
					+'</div>'
					+'<div class="ub-f1 ub-ver ub" style="margin-right:0.3em;margin-bottom:0.2em;">'
						+'<div class="umh2"></div>'
						+'<div id="${uid}" name="${uid}" class="ub-f1 min-w5 max-w5 tx-c ulev t-wh c-wh ver-c1" onclick="attention(this)" style="background-color:rgb(117, 113, 113);max-height: 1.7em;min-height:1.7em;">取消关注</div>'
					+'</div>'
					+'</div>'
				+'</div>';
var tmpl_fansFollow='<div class="ub max-h5" style="border-bottom:1px solid black;min-height:3.5em;">'
					+'<div id="${uid}" class="ub-f1 max-h4 max-w4 umar-a"onclick="PerCenter(this)"><img style="width:100%;" src="${avatar_url}"></div>'
					+'<div id="${uid}" class="ub-f3 min-w7 ub-ver umar-a" onclick="PerCenter(this)">'
						+'<div class="ub-f3 ub-ver" >'
						+'<div class="ub">${nickname}</div>'
						+'<div class="ub umh2 ">'
							+'<div class="ub-f1 umh2 ${sexPic}"></div>'
							+'<div class="ub-f2 ver-c ulev-1">粉丝:${follower_count}</div>'
						+'</div>'
					+'</div>'
					+'</div>'
					+'<div class="ub-f1 ub-ver ub" style="margin-right:0.3em;margin-bottom:0.2em;">'
						+'<div class="umh2"></div>'
						+'<div id="${uid}" name="${uid}" class="ub-f1 min-w5 max-w5 tx-c ulev t-wh c-wh ver-c1" onclick="attention(this)" style="background-color:rgb(117, 113, 113);max-height: 1.7em;min-height:1.7em;">取消关注</div>'
					+'</div>'
					+'</div>'
				+'</div>';
var tmpl_fansUnfollow='<div class="ub max-h5" style="border-bottom:1px solid black;min-height:3.5em;">'
					+'<div id="${uid}" class="ub-f1 max-h4 max-w4 umar-a"onclick="PerCenter(this)"><img style="width:100%;" src="${avatar_url}"></div>'
					+'<div id="${uid}" class="ub-f3 min-w7 ub-ver umar-a" onclick="PerCenter(this)">'
						+'<div class="ub-f3 ub-ver" >'
						+'<div class="ub">${nickname}</div>'
						+'<div class="ub umh2 ">'
							+'<div class="ub-f1 umh2 ${sexPic}"></div>'
							+'<div class="ub-f2 ver-c ulev-1">粉丝:${follower_count}</div>'
						+'</div>'
					+'</div>'
					+'</div>'
					+'<div class="ub-f1 ub-ver ub" style="margin-right:0.3em;margin-bottom:0.2em;">'
						+'<div class="umh2"></div>'
						+'<div id="${uid}" name="${uid}" class="ub-f1 min-w5 max-w5 tx-c ulev t-wh c-wh ver-c1" onclick="attention(this)" style="background-color:rgb(201, 9, 9);max-height: 1.7em;min-height:1.7em;">关 注</div>'
					+'</div>'
					+'</div>'
				+'</div>';
var tmpl_add_Fol='<div id="myFriend" name="myFriend" onclick="loadFriend(this,true)" class="ub umh5  unn c-wh tx-c ub-ver" style="border:1px solid #c5c5c5;background-color:#ebebeb; ">'
		+'<div ontouchstart="zy_touch('+'btn-act'+')" class="btn  uinn5 uc-a t-bla">更多</div>'
 +'</div>';
 var tmpl_add_Fan='<div id="myFans" name="myFans" onclick="loadFriend(this,true)" class="ub umh5  unn c-wh tx-c ub-ver" style="border:1px solid #c5c5c5;background-color:#ebebeb; ">'
		+'<div ontouchstart="zy_touch('+'btn-act'+')" class="btn  uinn5 uc-a t-bla">更多</div>'
 +'</div>';
 var tmpl_add_Mut='<div id="mutulFans" name="mutulFans" onclick="loadFriend(this,true)" class="ub umh5  unn c-wh tx-c ub-ver" style="border:1px solid #c5c5c5;background-color:#ebebeb; ">'
		+'<div ontouchstart="zy_touch('+'btn-act'+')" class="btn  uinn5 uc-a t-bla">更多</div>'
 +'</div>';
 
 
 function PerCenter(obj){
 	var OtherUid=obj.id;
	window.localStorage.setItem("is_me",false);
	window.localStorage.setItem("OtherUid",OtherUid);
	uexWindow.open("perCenter",0,"perCenter.html",4,"","",0,500);
 }
 
 /**
  * 
  * @param {Object} type
  * @param {Object} is_add
  */
function loadFriend(type,is_add){
	if (is_add) {
		type=type.id;
	}
	var url='';
	if(type=='myFriend'){
		url='http://www.diaoyumi.ourstu.com/api/profile/getFollowingList';
	}else if(type=='myFans'){
		url='http://www.diaoyumi.ourstu.com/api/profile/getFollowerList';
	}else{
		url='http://www.diaoyumi.ourstu.com/api/profile/getBifollowList';
	}
	var httpID=755;
	uexXmlHttpMgr.onData=function (inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				var len=zy_tmpl_count(json["list"]);
				if(is_add){
					var newlen=parseInt(window.localStorage[type+"offset"])+len;
					window.localStorage.setItem(type+"offset",newlen);
					uexXmlHttpMgr.close(httpID);
					if(len==0){
						uexWindow.toast(1,5,"没有更多的了",500);
					}
					stitchFriendTmpl(len,json,true,type);
				}else{
					window.localStorage.setItem(type+"offset",len);
					uexXmlHttpMgr.close(httpID);
					stitchFriendTmpl(len,json,false,type);
				}
				
				
			}else{
				uexWindow.toast(1,5,json["message"],500);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',url,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"uid",'1');
	if(is_add){
		uexXmlHttpMgr.setPostData(httpID,'0',"offset",window.localStorage[type+"offset"]);
		uexWindow.toast(1,5,"加载中",0);
	}
	uexXmlHttpMgr.send(httpID);
}
/**
 * 
 * @param {Object} len
 * @param {Object} json
 * @param {Object} is_add
 * @param {Object} type
 */
function stitchFriendTmpl(len,json,is_add,type){
	if(is_add){
		var btn_add=$$(type);
		btn_add.parentNode.removeChild(btn_add);
	}
	for(var i=0;i<len;i++){
				var obj=json["list"][i];
				if(obj["sex"]=="0"){
					obj["sexPic"]="imgBoy";
				}else{
					obj["sexPic"]="imgGirl";
				}
				if(type=='myFirend'){
					var friend=zy_tmpl_s(tmpl_followinf_friend,obj,zy_tmpl_count(obj));
				}else if(type=='myFans'){
					if(obj["is_followed"]){
						var friend=zy_tmpl_s(tmpl_fansFollow,obj,zy_tmpl_count(obj));
					}else{
						var friend=zy_tmpl_s(tmpl_fansUnfollow,obj,zy_tmpl_count(obj));
					}
				}else{
					var friend=zy_tmpl_s(tmpl_mutulFollow_friend,obj,zy_tmpl_count(obj));
				}
				//var friend=zy_tmpl_s(tmpl_mutulFollow_friend,obj,zy_tmpl_count(obj));
				var d=document.createElement('DIV');			
				d.innerHTML=friend;
				document.body.appendChild(d);
	}
	window.localStorage[type+"Friend"]=JSON.stringify(json);//将json对象转换成json字符串保存
	//var read=window.localStorage[type+"WeiboList"];
	var add=document.createElement('DIV');
				
	if(type=='myFirend'){
		var end=zy_tmpl_s(tmpl_add_Fol,obj,zy_tmpl_count(obj));
	}else if(type=='myFans'){
			var end=zy_tmpl_s(tmpl_add_Fan,obj,zy_tmpl_count(obj));
	}else{
			var end=zy_tmpl_s(tmpl_add_Mut,obj,zy_tmpl_count(obj));
	}
	add.innerHTML=end;
					
	document.body.appendChild(add);
	uexWindow.closeToast();
}
function attention(obj){
	
	var innerHtml=obj.innerHTML;
	innerHtml.replace(/[ ]/g,"");
	if(innerHtml=="关 注"){
		follow(obj,true);//true表示未关注 要关注 
		//以下是测试
		//gz.style.backgroundColor="rgb(117, 113, 113)";
		//gz.innerHTML="取消关注";
	}else{
		follow(obj,false);//false表示已关注要取消关注
		//以下是测试
		//gz.style.backgroundColor="rgb(201, 9, 9)";
		//gz.innerHTML="关 注";
	}
}

function follow(obj,type){
	var url='';
	if(type){
		url='http://www.diaoyumi.ourstu.com/api/profile/follow';
	}else{
		url='http://www.diaoyumi.ourstu.com/api/profile/unfollow';
	}
	var uid=obj.id;
	
	var httpID=996;
	
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				
				if(type){
					obj.style.backgroundColor="rgb(117, 113, 113)";
					obj.innerHTML="取消关注";
					uexWindow.toast(1,5,"关注成功",500);
				}else{
					obj.style.backgroundColor="rgb(201, 9, 9)";
					obj.innerHTML="关 注";
					uexWindow.toast(1,5,"取消关注成功",500);
				}
			}else{
				uexWindow.toast(1,5,json["massage"],500);
			}
			uexXmlHttpMgr.close(httpID);
		}
	}
	if(type){//true 是关注
		uexXmlHttpMgr.open(httpID,'post',url,3000);
		uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,'0',"uid",uid);
		uexXmlHttpMgr.send(httpID);
	}else{//false是取消关注
		uexXmlHttpMgr.open(httpID,'post',url,3000);
		uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,'0',"uid",uid);
		uexXmlHttpMgr.send(httpID);
	}
	
}